package com.zueb.web.servlet;

import com.zueb.pojo.NucleicAcid;
import com.zueb.service.NucleicAcidService;
import com.zueb.service.impl.NucleicAcidServiceImpl;
import com.zueb.util.ResponseWriterUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(name = "NucleicAcidServlet", value = "/NucleicAcidServlet")
public class NucleicAcidServlet extends HttpServlet {
    private NucleicAcidService nucleicAcidService = new NucleicAcidServiceImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //转发请求到doPost
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //转码
        request.setCharacterEncoding("UTF-8");
        //action表示用来区分是什么操作
        String action = request.getParameter("action");

        if (action.equals("addAcid")) {
            addAcid(request, response);
        } else if (action.equals("toShowAcid")) {
            toShowAcid(request, response);
        } else if (action.equals("ShowAllAcidByPage")) {
            ShowAllAcidByPage(request, response);
        }
    }


    /**
     * 带页码查询所有记录
     * @param request
     * @param response
     */
    private void ShowAllAcidByPage(HttpServletRequest request, HttpServletResponse response) {
        int curPage = Integer.parseInt(request.getParameter("curPage"));
        int pageSize = Integer.parseInt(request.getParameter("pageSize"));
        int facultyId = Integer.parseInt(request.getParameter("facultyId"));
        int classId = Integer.parseInt(request.getParameter("classId"));

        List<NucleicAcid> nucleicAcids = nucleicAcidService.showAcidByPage(curPage, pageSize, facultyId, classId);
        int count = nucleicAcidService.showAcidCount(facultyId, classId);

        Map<String, Object> map = new HashMap<>();
        map.put("nucleicAcids", nucleicAcids);
        map.put("count", count);

        ResponseWriterUtils.writer(map, response);
    }

    /**
     * 学生查询核酸检测登记记录
     * @param request
     * @param response
     */
    private void toShowAcid(HttpServletRequest request, HttpServletResponse response) {
        String userNumber = request.getParameter("userNumber");
        NucleicAcid nucleicAcid = new NucleicAcid();
        nucleicAcid.setUserNumber(userNumber);
        List<NucleicAcid> nucleicAcids = nucleicAcidService.toShowAcid(nucleicAcid);

        System.out.println("nucleicAcids = " + nucleicAcids);
        Map<String, Object> map = new HashMap<>();
        map.put("nucleicAcids", nucleicAcids);

        ResponseWriterUtils.writer(map, response);
    }

    /**
     * 添加核酸检记录
     * @param request
     * @param response
     */
    private void addAcid(HttpServletRequest request, HttpServletResponse response) {

        String number = request.getParameter("number");
        String datetime = request.getParameter("datetime");
        String result = request.getParameter("result");

        NucleicAcid nucleicAcid = new NucleicAcid();
        nucleicAcid.setUserNumber(number);
        nucleicAcid.setTime(datetime);
        if (request.equals("ture")) {
            nucleicAcid.setResult(2);//阳性
        } else {
            nucleicAcid.setResult(1);//阴性
        }

        System.out.println("nucleicAcid = " + nucleicAcid);

        int flag = nucleicAcidService.addAcid(nucleicAcid);
        System.out.println("flag = " + flag);
        //传回参数
        ResponseWriterUtils.writerFlag(flag, response);
    }
}
